function [grade]=segment_map_grade(seg_indices,polling_table,sum_coef_table)

% This function returns an internal grade for the system's output. the grade is based on the sum of confidence 
% coefficients taken from the classifier and arranged in a table.

% Function Inputs:
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% seg_indices - cell that contains the indices of each segment.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% polling_table - a table in size of labelsXseg_count containes the  polling votes (max votes count).
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% sum_coef_table - a table in size of labelsXseg_count containes the  sum_value votes (sum of 
% confidence values).
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

% Function Outputs:
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% grade - a final grade of the system.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

seg_size_vector=cellfun('size',seg_indices,1); % getting the vector of segments sizes
[na,winner_labels_vector]=max(polling_table); % getting the vector of winners by polling
abs_sum_coef_table=abs(sum_coef_table);

% for each polling winner - get his sum_value value
sum_value_winner_labels_vector=abs_sum_coef_table(sub2ind(size(abs_sum_coef_table),winner_labels_vector,1:size(abs_sum_coef_table,2)));

% dividing the winners' sum_value by the total sum_values of each segment
winners_coefficient_rate=sum_value_winner_labels_vector./sum(abs_sum_coef_table,1);
winners_coefficient_rate(find(isnan(winners_coefficient_rate)==1))=1;

% the grade is set by the mean of the winners_coefficient_rate * the pct of defined segments
grade=mean(winners_coefficient_rate)*(sum(seg_size_vector(winner_labels_vector~=5))/sum(seg_size_vector));
